package com.clever.config;

import com.clever.config.handler.ChatWebSocketHandler;
import com.clever.service.ChatMessageService;
import com.clever.service.ConversationService;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import javax.annotation.Resource;

/**
 * @Author xixi
 * @Date 2024-04-08 11:06
 **/
@Configuration
@EnableWebSocket
public class RabbitMQConfig implements WebSocketConfigurer {
    public final static String CHAT_QUEUE = "LYLChatQueue";
    @Resource
    private ConversationService conversationService;
    @Resource
    private ChatMessageService chatMessageService;
    @Bean
    public Queue useChatQueue() {
        return new Queue(CHAT_QUEUE);
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        webSocketHandlerRegistry.addHandler(new ChatWebSocketHandler(conversationService,chatMessageService), "/chat").setAllowedOrigins("*");
    }
}
